<template>
  <div class="addProduct">
      <input type="text" v-model="newProductName" placeholder="输入新商品名称">
      <button @click="addProduct">添加商品</button>
    </div>
  <div class="shopping-cart">
    <ul class="product-list">
      <li v-for="(product, index) in products" :key="index" class="product-item">
        <div class="product-info">
          <span class="product-name">{{ product.name }}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <span class="product-price">￥ {{ product.price }}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
        <div class="quantity-controls">
          <button @click="decreaseQuantity(index)" class="quantity-btn">-</button>
          <span class="quantity">{{ product.quantity }}</span>
          <button @click="increaseQuantity(index)" class="quantity-btn">+</button>
          <button @click="removeProduct(index)" class="remove-btn">删除</button>
        </div>
      </li>
    </ul>

    <div class="total">总价: ￥ {{ total }}</div>
    <div class="goBuy">购买</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [
        { name: '商品1', price: 10, quantity: 1 },
        { name: '商品2', price: 20, quantity: 1 },
        { name: '商品3', price: 30, quantity: 1 }
      ],
      newProductName: ''
    };
  },
  computed: {
    total() {
      return this.products.reduce((acc, product) => {
        return acc + product.price * product.quantity;
      }, 0);
    }
  },
  methods: {
    increaseQuantity(index) {
      this.products[index].quantity++;
    },
    decreaseQuantity(index) {
      if (this.products[index].quantity > 1) {
        this.products[index].quantity--;
      }
    },
    addProduct() {
      if (this.newProductName.trim() !== '') {
        const newProduct = { name: this.newProductName, price: 50, quantity: 1 };
        this.products.push(newProduct);
        this.newProductName = ''; // Clear the input field after adding the product
      }
    },
    removeProduct(index) {
      this.products.splice(index, 1);
    }
  }
};
</script>

<style>
.product-list {
  list-style: none;
  padding: 0;
}

.product-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.product-info {
  flex: 1;
}

.quantity-controls {
  display: flex;
  align-items: center;
}

.quantity-btn {
  background-color: #007bff;
  color: #fff;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
  border-radius: 5px;
}

.remove-btn {
  background-color: #dc3545;
  color: #fff;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
  border-radius: 5px;
  margin-left: 10px;
}

.total {
  margin-top: 20px;
  font-size: 18px;
  font-weight: bold;
  border: 1px solid;
  border-radius: 6px;
}

.goBuy {
  margin-top: 20px;
  font-size: 18px;
  font-weight: bold;
  border: 1px solid;
  border-radius: 6px;
  background-color: #b2cfee;
}

.addProduct {
  margin-top: 10px;
}

.addProduct input {
  padding: 5px;
  margin-right: 10px;
}

.addProduct button {
  background-color: #28a745;
  color: #fff;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
  border-radius: 5px;
}
</style>
